<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="__ADMIN__css/font.css">
    <link rel="stylesheet" href="__ADMIN__css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="__ADMIN__lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="__ADMIN__js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>

    <![endif]-->
</head>

<body class="layui-anim layui-anim-up">

<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
            <input style="width: 200px" type="text" name="name"  placeholder="请输入名称" autocomplete="off" class="layui-input">
            <div class="layui-input-inline">
                <select id="type" name="type" class="valid">
                    <option value="0">选择分类</option>
                    {volist name="type" id="v"}
                    <option value="{$v.id}">{$v.name}</option>
                    {/volist}
                </select>
            </div>
            <button class="layui-btn" type="button"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
            <button class="layui-btn layui-btn-danger fr" type="button" id="del"><i class="layui-icon"></i>批量删除</button>
            <button class="layui-btn layui-btn-normal fr" type="button" id="copy"><i class="layui-icon"></i>批量复制</button>
            <button class="layui-btn layui-btn-warm  fr" id="refresh" type="button"><i class="layui-icon ">&#xe63f;</i>刷新</button>
            <a class="layui-btn fr" href="/admin/product/add"><i class="layui-icon"></i>添加</a>
        </form>
    </div>

    <table class="" lay-filter="test" id="table">

    </table>
</div>
<div class="layui-input-inline xiala" style="display: none">
    <select id="type1" name="type1" class="valid">
        <option value="0">请选择目标分类</option>
        {volist name="type" id="v"}
        <option value="{$v.id}">{$v.name}</option>
        {/volist}
    </select>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn  layui-btn-xs"  lay-event="edit3">首页推荐</a>
    <a class="layui-btn  layui-btn-xs"  lay-event="edit2">预览</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs"  lay-event="edit1">是否上架</a>

    <a class="layui-btn layui-btn-normal layui-btn-xs"  lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    //获取列表接口
    var listurl = '/admin/Parts/getList';
    //添加页面接口
    var addurl = "/admin/Parts/add";
    //修改页面接口
    var upurl = '/admin/product/edit/id/';
    //删除接口
    var delurl = '/admin/product/delProduct';
    //排序接口
    var sortUrl = '/admin/product/sort';
    //上架接口
    var auditurl = '/admin/product/shangjia'
    //推荐接口
    var tuijianurl = '/admin/product/indextuijian'
    layui.use('table', function(){
        var table = layui.table;
        table.render({
            elem: '#table'
            ,method:'post'
            ,height: 'full-200'
            ,id:'idTest'
            ,url: '/admin/product/lists' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                ,{type: 'checkbox'}
                ,{field: 'type_name', title: '产品分类',sort: true}
                ,{field: 'name', title: '产品名称'}
                ,{field: 'view_num', title: '产品浏览量',sort: true}
                ,{field: 'create_time', title: '创建时间',sort: true}
                ,{field: 'flag', title: '上架状态',sort: true}
                ,{field: 'index', title: '是否首页推荐',sort: true}
                ,{field: 'sort_nav',fixed: 'right', title:'导航排序' ,width:100,style:'cursor: pointer;', event: 'sort2',sort: true}
                ,{field: 'sort_index',fixed: 'right', title:'首页排序' ,width:100,style:'cursor: pointer;', event: 'sort1',sort: true}
                ,{field: 'sort',fixed: 'right', title:'列表排序' ,width:100,style:'cursor: pointer;', event: 'sort',sort: true}
                ,{fixed: 'right',width:290,title: '操作', align:'center', toolbar: '#barDemo'}
            ]]
        });

        table.on('tool(test)', function(obj){
            var data = obj.data;
            //监听删除
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: delurl,
                        data: {'id':data.id},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                layer.msg(success.msg);
                                obj.del();
                                layer.close(index);
                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });
                });
            }
            if(obj.event === 'sort'){

                layer.prompt({
                    formType: 2
                    ,title: '修改 ID 为 ['+ data.id +'] 的排序'
                    ,value: data.sort
                }, function(value, index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: sortUrl,
                        data:{
                            'id':data.id,
                            'sort':value
                        },
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                //同步更新表格和缓存对应的值
                                obj.update({
                                    sort: value
                                });
                                layer.msg(success.msg, {
                                    time: 1000
                                });
                            }else {
                                layer.msg(success.msg);
                            }
                        }
                    });

                });
            }
            if(obj.event === 'sort1'){

                if(data.index=='未推荐'){
                    layer.msg('该产品未首页推荐！');
                    return false;
                }
                layer.prompt({
                    formType: 2
                    ,title: '修改 ID 为 ['+ data.id +'] 的首页排序'
                    ,value: data.sort_index
                }, function(value, index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: '/admin/product/sort1',
                        data:{
                            'id':data.id,
                            'sort_index':value
                        },
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                //同步更新表格和缓存对应的值
                                obj.update({
                                    sort_index: value
                                });
                                layer.msg(success.msg, {
                                    time: 1000
                                });
                            }else {
                                layer.msg(success.msg);
                            }
                        }
                    });

                });
            }
            if(obj.event === 'sort2'){
                layer.prompt({
                    formType: 2
                    ,title: '修改 ID 为 ['+ data.id +'] 的导航排序'
                    ,value: data.sort_nav
                }, function(value, index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: '/admin/product/sort2',
                        data:{
                            'id':data.id,
                            'sort_nav':value
                        },
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                //同步更新表格和缓存对应的值
                                obj.update({
                                    sort_nav: value
                                });
                                layer.msg(success.msg, {
                                    time: 1000
                                });
                            }else {
                                layer.msg(success.msg);
                            }
                        }
                    });

                });
            }
            //监听上架
            if(obj.event === 'edit1'){
                layer.confirm('请选择是否上架', {
                    btn: ['是','否'] //按钮
                }, function(){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: auditurl,
                        data: {'id':data.id,'flag':1},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                obj.update({
                                    flag: '已上架'
                                });
                                layer.msg(success.msg);
                                layer.close(index);

                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });
                }, function(){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: auditurl,
                        data: {'id':data.id,'flag':2},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                obj.update({
                                    flag: '未上架'
                                });
                                layer.msg(success.msg);
                                layer.close(index);

                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });
                });

            }
            //监听预览
            if(obj.event === 'edit2'){
                var url1 = '/product/'+data.id+'.html';
                window.open(url1);
            }
            //监听推荐
            if(obj.event === 'edit3'){
                layer.confirm('请选择是否推荐', {
                    btn: ['是','否'] //按钮
                }, function(){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: tuijianurl,
                        data: {'id':data.id,'index':1},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                obj.update({
                                    index: '已推荐'
                                });
                                layer.msg(success.msg);
                                layer.close(index);

                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });
                }, function(){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: tuijianurl,
                        data: {'id':data.id,'index':2},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                obj.update({
                                    index: '未推荐'
                                });
                                layer.msg(success.msg);
                                layer.close(index);

                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });
                });
            }
            //监听修改
            if(obj.event === 'edit'){
                var url = upurl+data.id;
                location.href = url
            }
        });
        //刷新表格
        $('#refresh').click(function () {
            table.reload('idTest', {
                url: '/admin/product/lists'
                ,where: {'name':0,'type':0} //设定异步数据接口的额外参数
                //,height: 300
            });
        });
        var fuzhi = '';
        //批量复制
        $('#copy').click(function(){
            var checkStatus = table.checkStatus('idTest');
            if(checkStatus.data.length===0){
                layer.msg("请选择要复制的行");
                return false;
            }
            //捕获页
            fuzhi = layer.open({
                type: 1,
                shade: false,
                title: false, //不显示标题
                content: $('.xiala'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                cancel: function(){

                }
            });

        })
        $("#type1").bind("change",function () {
            var checkStatus = table.checkStatus('idTest');
            var type_id = $(this).val();
            if(type_id == 0){
                layer.msg("请选择一个目标分类！");
            }else{
                var data1 = checkStatus.data;
                var data2 = [];
                for (var i=0;i<data1.length;i++)
                {
                    data2[i] = data1[i].id;
                }
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: '/admin/product/copyProduct',
                    data: {
                        data:data2,
                        type_id:type_id
                    },
                    success: function (res) {
                        var success = JSON.parse(res);
                        if(success.code==200){
                            console.log(success)
                            layer.close(fuzhi);
                            layer.msg(success.msg)
                        }else {
                            layer.msg(success.msg)
                        }

                    }
                });
            }
        });

        //批量删除
        $('#del').click(function () {
            var checkStatus = table.checkStatus('idTest');
            if(checkStatus.data.length===0){
                layer.msg("请选择要删除的行");
                return false;
            }
            layer.confirm('真的删除行么', function(index){
                var data1 = checkStatus.data;
                var data2 = [];
                for (var i=0;i<data1.length;i++)
                {
                    data2[i] = data1[i].id;
                }
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: '/admin/product/delAllProduct',
                    data: {data:data2},
                    success: function (res) {
                        var success = JSON.parse(res);
                        if(success.code==200){
                            layer.msg(success.msg);
                            table.reload('idTest', {
                                url: '/admin/product/lists'
                                ,where: {'name':0}  //设定异步数据接口的额外参数
                                //,height: 300
                            });
                        }else {
                            layer.msg(success.msg)
                        }

                    }
                });
            });
        });
        //监听搜索提交
        var form = layui.form,layer = layui.layer;
        form.on('submit(sreach)', function(data){
            table.reload('idTest', {
                url: '/admin/product/lists'
                ,where: data.field //设定异步数据接口的额外参数
                //,height: 300
            });
        });

    });


</script>
<script>

</script>
</body>

</html>